<template>
  <span>实习经验</span>
  <el-button text @click="isAddIE = true">添加实习经验</el-button>
  <el-dialog v-model="isAddIE" title="添加实习经验">
    <el-form ref="formRef" :model="addIEForm" label-width="100px">
      <el-form-item label="企业名称" prop="name">
        <el-input v-model="addIEForm.enterpriseName"></el-input>
      </el-form-item>
      <el-form-item label="详情" prop="description">
        <el-input v-model="addIEForm.description" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="岗位" prop="role">
        <el-input v-model="addIEForm.position"></el-input>
      </el-form-item>
      <el-form-item label="开始日期" prop="startDate">
        <el-date-picker
            v-model="addIEForm.startTime"
            placeholder="选择日期"
            type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endDate">
        <el-date-picker
            v-model="addIEForm.endTime"
            placeholder="选择日期"
            type="date"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="addIE(addIEForm)">提交</el-button>
        <el-button type="info" @click="isAddIE = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
  <div v-for="ie in IEList" :key="ie.id">
    <el-descriptions :column="2" :title="ie.enterpriseName" border class="des">
      <template #extra>
        <el-button v-show="!modifyDict[ie.id]" type="primary" @click="modifyDict[ie.id] = true">编辑</el-button>
        <el-popconfirm
            v-show="!modifyDict[ie.id]"
            cancel-button-text="取消"
            confirm-button-text="确定"
            title="确定删除吗?"
            @confirm="deleteIE(ie)"
        >
          <template #reference>
            <el-button type="danger">删除</el-button>
          </template>
        </el-popconfirm>
        <el-button v-show="modifyDict[ie.id]" type="info" @click="modifyDict[ie.id] = false">取消</el-button>
        <el-button v-show="modifyDict[ie.id]" type="success" @click="updateIE(ie)">保存</el-button>
      </template>
      <el-descriptions-item label="公司名称">
        <el-input v-show="modifyDict[ie.id]" v-model="ie.enterpriseName"/>
        <span v-show="!modifyDict[ie.id]">{{ ie.enterpriseName }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="岗位">
        <el-input v-show="modifyDict[ie.id]" v-model="ie.position"/>
        <span v-show="!modifyDict[ie.id]">{{ ie.position }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="开始时间">
        <div v-show="modifyDict[ie.id]">
          <el-date-picker
              v-model="ie.startTime"
              placeholder="请选择"
              type="date"
          />
        </div>
        <span v-show="!modifyDict[ie.id]">{{
            ie.startTime instanceof Date ? ie.startTime.toLocaleDateString() : ie.startTime.substr(0, 10)
          }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="结束时间">
        <div v-show="modifyDict[ie.id]">
          <el-date-picker
              v-model="ie.endTime"
              placeholder="请选择"
              type="date"
          />
        </div>
        <span v-show="!modifyDict[ie.id]">{{
            ie.endTime instanceof Date ? ie.endTime.toLocaleDateString() : ie.endTime.substr(0, 10)
          }}</span>
      </el-descriptions-item>
      <el-descriptions-item label="详情">
        <el-input v-show="modifyDict[ie.id]" v-model="ie.description" type="textarea"/>
        <div v-show="!modifyDict[ie.id]">{{ ie.description }}</div>
      </el-descriptions-item>
    </el-descriptions>
    <el-divider/>
  </div>
</template>

<script lang="ts" setup>
import type {
  InternshipExperienceInterface,
  ProjectExperienceInterface,
  ResponseInterface,
  StudentInterface
} from "@/model/type";
import {onMounted, reactive, ref} from "vue";
import {
  addInternshipExperienceAPI,
  deleteInternshipExperienceAPI,
  getInternshipExperiencesAPI,
  updateInternshipExperienceAPI
} from "@/api/ResumeAPI";
import {ElMessage} from "element-plus";

const isAddIE = ref<boolean>(false)
const addIEForm = reactive<InternshipExperienceInterface>({
  description: "",
  endTime: "",
  enterpriseName: "",
  id: "",
  position: "",
  resumeId: "",
  startTime: "",
  studentId: ""
})
const IEList = reactive<Array<InternshipExperienceInterface>>([]);
const modifyDict = reactive({"id": false})
const resumeId: string | null = localStorage.getItem("resumeId")
const student: StudentInterface = JSON.parse(localStorage.getItem("user"))

async function addIE(internshipExperience: InternshipExperienceInterface) {
  internshipExperience.resumeId = resumeId
  internshipExperience.studentId = student.id
  const r: ResponseInterface = await addInternshipExperienceAPI(internshipExperience, localStorage.getItem("token"))
  if (r.code == 200) {
    ElMessage.success("添加成功")
    IEList.push(r.data)
    Object.assign(modifyDict, {[r.data.id]: false})
    Object.assign(addIEForm, {
      description: "",
      endTime: "",
      id: "",
      enterpriseName: "",
      resumeId: "",
      position: "",
      startTime: "",
      studentId: ""
    })
    isAddIE.value = false
  }
}

async function deleteIE(pe: ProjectExperienceInterface) {
  const r: ResponseInterface = await deleteInternshipExperienceAPI(pe, localStorage.getItem("token"))
  if (r.code == 200) {
    ElMessage.success("删除成功")
    IEList.splice(IEList.indexOf(pe), 1)
  }
}

async function updateIE(pe: ProjectExperienceInterface) {
  const r: ResponseInterface = await updateInternshipExperienceAPI(pe, localStorage.getItem("token"))
  if (r.code == 200) {
    ElMessage.success("修改成功")
    modifyDict[pe.id] = false
  }
}

onMounted(async () => {
  const r: ResponseInterface = await getInternshipExperiencesAPI(resumeId, localStorage.getItem("token"))
  if (r.code == 200) {
    for (let i = 0; i < r.data.length; i++) {
      IEList.push(r.data[i])
      Object.assign(modifyDict, {[r.data[i].id]: false})
    }
  }
})
</script>
<style scoped>
.des {
  margin-top: 20px;
}
</style>